package com.pactera.jep.service.yapin.coll.controller;

import com.pactera.jep.core.exception.ServiceException;
import com.pactera.jep.orm.Filter;
import com.pactera.jep.orm.Order;
import com.pactera.jep.orm.Page;
import com.pactera.jep.service.yapin.coll.model.*;
import com.pactera.jep.service.yapin.coll.service.*;
import com.pactera.jep.service.yapin.coll.vo.ColCollBaseInfoCheckVO;
import com.pactera.jep.service.yapin.coll.vo.CollBaseListVO;
import com.pactera.jep.service.yapin.coll.vo.ParamInfoListVO;
import com.pactera.jep.service.yapin.common.ResultCode;
import com.pactera.jep.web.code.WebCode;
import com.pactera.jep.web.controller.CrudController;
import com.pactera.jep.web.entity.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *
 * @Author YouName
 * @Date 2020-6-29 9:56:19
 */
@RestController
@RequestMapping("/collInfo")
@Api(description = "押品基础信息表")
public class CollInfoController extends CrudController<CollBaseInfo> {

    private static Logger logger = LoggerFactory.getLogger(CollInfoController.class);

    @Autowired
    CollBaseInfoService collBaseInfoService;

    @Autowired
    ValueInfoService valueInfoService;

    @Autowired
    InsurenceInfoService insurenceInfoService;

    @Autowired
    WarrantInfoService warrantInfoService;

    @Autowired
    RentalInfoService rentalInfoService;

    @Autowired
    AccidentInfoService accidentInfoService;

    @Autowired
    CoownerService coownerService;

    @PostMapping("/getCollBaseInfoList")
    @ApiOperation("查询押品信息列表")
    public Map<String,Object> getCollBaseInfoList(@RequestBody CollBaseListVO collBaseListVO) {
        return collBaseInfoService.getCollBaseInfoList(collBaseListVO);
    }

    /**
     * 新增押品信息
     */
    @PostMapping("/checkBaseInfo")
    @ApiOperation("插入押品基础信息表")
    public Result checkBaseInfo(@RequestBody ColCollBaseInfoCheckVO colCollBaseInfoCheckVO) {
        try {
            Assert.hasLength(colCollBaseInfoCheckVO.getCltTpCd(),"押品类型不可为空");
            Assert.hasLength(colCollBaseInfoCheckVO.getCltNm(),"押品名称不可为空");
            Assert.hasLength(colCollBaseInfoCheckVO.getUniqWarrantTp(),"权证类型不可为空");
            Assert.hasLength(colCollBaseInfoCheckVO.getUniqWarrantNo(),"权证编号不可为空");
            return Result.success(collBaseInfoService.checkBaseInfo(colCollBaseInfoCheckVO));
        }catch (IllegalArgumentException e){
            e.printStackTrace();
            return Result.error(ResultCode.PARAM_ERROR,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(ResultCode.UNKNOWN_ERROR,"系统异常");
        }

    }

    /**
     * 为列表页面做数据准备
     */
    @Override
    protected void prepareQuery(Page<CollBaseInfo> page, List<Filter> filters, List<Order> orders, HttpServletRequest request) {
        orders.add(Order.desc("createdTime"));
        collBaseInfoService.query(page, filters, orders);
    }

    @GetMapping("/getBaseInfo")
    @ApiOperation("查询押品基础信息表")
    public Result getBaseInfo(@RequestParam String cltId) {
        try {
            Assert.hasLength(cltId,"id丢失");
            return Result.success(collBaseInfoService.getBaseInfo(cltId));
        }catch (IllegalArgumentException e){
            e.printStackTrace();
            return Result.error(ResultCode.PARAM_ERROR,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(ResultCode.UNKNOWN_ERROR,"系统异常");
        }

    }
    @GetMapping("/getBaseInfoById")
    @ApiOperation("查询押品基础信息表")
    public Result getBaseInfoById(@RequestParam String cltId) {
        try {
            Assert.hasLength(cltId,"id丢失");
            return Result.success(collBaseInfoService.getBaseInfoById(cltId));
        }catch (IllegalArgumentException e){
            e.printStackTrace();
            return Result.error(ResultCode.PARAM_ERROR,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(ResultCode.UNKNOWN_ERROR,"系统异常");
        }

    }


    @PostMapping("/updateBaseInfo")
    @ApiOperation("更新押品基础信息表")
    public Result updateBaseInfo(@RequestBody CollBaseInfo collBaseInfo) {
        try {
            Assert.hasLength(collBaseInfo.getCltId(),"主键丢失");
            return Result.success(collBaseInfoService.updateBaseInfo(collBaseInfo));
        }catch (IllegalArgumentException e){
            e.printStackTrace();
            return Result.error(ResultCode.PARAM_ERROR,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(ResultCode.UNKNOWN_ERROR,"系统异常");
        }

    }

    @PostMapping("/addOrupdateValueInfo")
    @ApiOperation("新增或更新押品价值信息")
    public Result addOrupdateValueInfo(@RequestBody ValueInfo valueInfo) {
        try {
            Assert.hasLength(valueInfo.getCltId(),"主键丢失");
            return Result.success(collBaseInfoService.addOrupdateValueInfo(valueInfo));
        }catch (IllegalArgumentException e){
            e.printStackTrace();
            return Result.error(ResultCode.PARAM_ERROR,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(ResultCode.UNKNOWN_ERROR,"系统异常");
        }

    }

    @PostMapping("/getValueInfoList")
    @ApiOperation("查询押品价值信息列表")
    public Map<String,Object> getValueInfoList(@RequestBody ParamInfoListVO valueInfoListVO) {
        Assert.hasLength(valueInfoListVO.getCltId(),"押品编号丢失");
        return collBaseInfoService.getValueInfoList(valueInfoListVO);

    }

    @GetMapping("/getValueInfo")
    @ApiOperation("查询押品价值信息")
    public Result getValueInfo(@RequestParam String valueinfoId) {
        try {
            Assert.hasLength(valueinfoId,"id丢失");
            return Result.success(collBaseInfoService.getValueInfo(valueinfoId));
        }catch (IllegalArgumentException e){
            e.printStackTrace();
            return Result.error(ResultCode.PARAM_ERROR,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(ResultCode.UNKNOWN_ERROR,"系统异常");
        }
    }
    @GetMapping("/getValueInfoByCltId")
    @ApiOperation("通过押品主键查询押品价值信息")
    public Map<String, Object> getValueInfoByCltId(@RequestParam String cltId) {
        Assert.hasLength(cltId,"押品主键丢失");
        Map<String, Object> result = collBaseInfoService.getValueInfoByCltId(cltId);
        return result;
    }

    @PostMapping("/saveValueInfo")
    @ApiOperation("保存押品价值信息")
    public Result saveValueInfo(@RequestBody ValueInfo valueInfo) {
        return Result.success(valueInfoService.insert(valueInfo));//新增
    }

    @PostMapping("/updValueInfo")
    @ApiOperation("修改押品价值信息")
    public Result updValueInfo(@RequestBody ValueInfo valueInfo) {
        return Result.success(valueInfoService.update(valueInfo));//修改
    }

    @PostMapping("/update")
    @ApiOperation("更新押品基本信息")
    public Map<String, Object> insert(@RequestBody CollBaseInfo collBaseInfo) {
        Map<String, Object> tips = new HashMap<String, Object>();

        Boolean isExist = collBaseInfoService.exists(collBaseInfo.getCltId());
        try {
            if (isExist) {
                collBaseInfoService.update(collBaseInfo);
            } else {
                tips.put(WebCode.SUCCESS, false);
                tips.put(WebCode.MSG, "系统中不存在对应的押品!");
                return tips;
            }
            tips.put(WebCode.SUCCESS, true);
            tips.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
        } catch (ServiceException e) {
            logger.error(e.getMessage(),e);
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_SAVE);
        }
        return tips;
    }

    @GetMapping("/removeValueInfo")
    @ApiOperation("删除押品价值信息")
    public Result removeValueInfo(@RequestParam String valueinfoId) {
        return Result.success(valueInfoService.deleteByPrimaryKey(valueinfoId));
    }

    @PostMapping("/deleteValueInfo")
    @ApiOperation("删除押品价值信息")
    public Result deleteValueInfo(@RequestBody List<String> valueinfoIds) {
        try {
            return Result.success(collBaseInfoService.deleteValueInfo(valueinfoIds));
        }catch (IllegalArgumentException e){
            e.printStackTrace();
            return Result.error(ResultCode.PARAM_ERROR,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(ResultCode.UNKNOWN_ERROR,"系统异常");
        }
    }

    @GetMapping("/getInsuranceInfoByCltNo")
    @ApiOperation("通过押品主键查询押品保险信息")
    public Map<String, Object> getInsuranceInfoByCltNo(@RequestParam String cltNo) {
        Assert.hasLength(cltNo,"押品主键丢失");
        Map<String, Object> result = insurenceInfoService.getInsuranceInfoByCltNo(cltNo);
        return result;
    }

    @PostMapping("/saveInsuranceInfo")
    @ApiOperation("保存/修改押品保险信息")
    public Result saveInsuranceInfo(@RequestBody InsurenceInfo InsurenceInfo) {
        //通过ID是否存在判断是新增还是修改
        Boolean isExist = insurenceInfoService.exists(InsurenceInfo.getInsurenceinfoId());
        if (isExist) {
            return Result.success(insurenceInfoService.update(InsurenceInfo));//修改
        } else {
            return Result.success(insurenceInfoService.insert(InsurenceInfo));//新增
        }
    }

    @GetMapping("/removeInsuranceInfo")
    @ApiOperation("删除押品保险信息")
    public Result removeInsuranceInfo(@RequestParam String insurenceinfoId) {
        return Result.success(insurenceInfoService.deleteByPrimaryKey(insurenceinfoId));
    }

    @PostMapping("/addOrUpdateInsurenceInfo")
    @ApiOperation("保存或修改押品保险信息")
    public Result addOrUpdateInsurenceInfo(@RequestBody InsurenceInfo insurenceInfo) {
        try {
            Assert.hasLength(insurenceInfo.getCltId(),"押品编号丢失");
            return Result.success(collBaseInfoService.addOrUpdateInsurenceInfo(insurenceInfo));
        }catch (IllegalArgumentException e){
            e.printStackTrace();
            return Result.error(ResultCode.PARAM_ERROR,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(ResultCode.UNKNOWN_ERROR,"系统异常");
        }

    }

    @PostMapping("/getInsurenceInfoList")
    @ApiOperation("查询押品保险信息列表")
    public Map<String,Object> getInsurenceInfoList(@RequestBody ParamInfoListVO valueInfoListVO) {
        Assert.hasLength(valueInfoListVO.getCltId(),"押品编号丢失");
        return collBaseInfoService.getInsurenceInfoList(valueInfoListVO);

    }

    @GetMapping("/getInsurenceInfo")
    @ApiOperation("查询押品保险信息")
    public Result getInsurenceInfo(@RequestParam String insurenceinfoId) {
        try {
            Assert.hasLength(insurenceinfoId,"id丢失");
            return Result.success(collBaseInfoService.getInsurenceInfo(insurenceinfoId));
        }catch (IllegalArgumentException e){
            e.printStackTrace();
            return Result.error(ResultCode.PARAM_ERROR,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(ResultCode.UNKNOWN_ERROR,"系统异常");
        }

    }

    @PostMapping("/deleteInsurenceInfo")
    @ApiOperation("删除押品保险信息")
    public Result deleteInsurenceInfo(@RequestBody List<String> insurenceinfoIds) {
        try {
            return Result.success(collBaseInfoService.deleteInsurenceInfo(insurenceinfoIds));
        }catch (IllegalArgumentException e){
            e.printStackTrace();
            return Result.error(ResultCode.PARAM_ERROR,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(ResultCode.UNKNOWN_ERROR,"系统异常");
        }
    }

    @PostMapping("/saveWarrantInfo")
    @ApiOperation("保存押品权证信息")
    public Result saveWarrantInfo(@RequestBody WarrantInfo warrantInfo) {
        return Result.success(warrantInfoService.insert(warrantInfo));//新增
    }

    @PostMapping("/updWarrantInfo")
    @ApiOperation("修改押品权证信息")
    public Result updWarrantInfo(@RequestBody WarrantInfo warrantInfo) {
        return Result.success(warrantInfoService.update(warrantInfo));//修改
    }

    @PostMapping("/addOrUpdateWarrantInfo")
    @ApiOperation("保存或修改押品权证信息")
    public Result addOrUpdateWarrantInfo(@RequestBody WarrantInfo warrantInfo) {
        try {
            Assert.hasLength(warrantInfo.getCltId(),"押品编号丢失");
            return Result.success(collBaseInfoService.addOrUpdateWarrantInfo(warrantInfo));
        }catch (IllegalArgumentException e){
            e.printStackTrace();
            return Result.error(ResultCode.PARAM_ERROR,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(ResultCode.UNKNOWN_ERROR,"系统异常");
        }

    }

    @PostMapping("/getWarrantInfoList")
    @ApiOperation("查询押品权证信息列表")
    public Map<String,Object> getWarrantInfoList(@RequestBody ParamInfoListVO valueInfoListVO) {
        Assert.hasLength(valueInfoListVO.getCltId(),"押品编号丢失");
        return collBaseInfoService.getWarrantInfoList(valueInfoListVO);

    }

    @GetMapping("/getWarrantInfo")
    @ApiOperation("查询押品权证信息")
    public Result getWarrantInfo(@RequestParam String warrantinfoId) {
        try {
            Assert.hasLength(warrantinfoId,"id丢失");
            return Result.success(collBaseInfoService.getWarrantInfo(warrantinfoId));
        }catch (IllegalArgumentException e){
            e.printStackTrace();
            return Result.error(ResultCode.PARAM_ERROR,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(ResultCode.UNKNOWN_ERROR,"系统异常");
        }

    }

    @GetMapping("/getWarrantInfoByCltId")
    @ApiOperation("通过押品主键查询押品权证信息")
    public Map<String, Object> getWarrantInfoByCltId(@RequestParam String cltId) {
        Assert.hasLength(cltId,"押品主键丢失");
        Map<String, Object> result = collBaseInfoService.getWarrantInfoByCltId(cltId);
        return result;
    }

    @GetMapping("/removeWarrantInfo")
    @ApiOperation("删除押品权证信息")
    public Result removeWarrantInfo(String warrantinfoId) {
        return Result.success(warrantInfoService.deleteByPrimaryKey(warrantinfoId));
    }

    @PostMapping("/addOrUpdateRentalInfo")
    @ApiOperation("保存或修改押品租赁信息")
    public Result addOrUpdateRentalInfo(@RequestBody RentalInfo rentalInfo) {
        try {
//            Assert.hasLength(rentalInfo.getCltId(),"押品编号丢失");
            return Result.success(collBaseInfoService.addOrUpdateRentalInfo(rentalInfo));
        }catch (IllegalArgumentException e){
            e.printStackTrace();
            return Result.error(ResultCode.PARAM_ERROR,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(ResultCode.UNKNOWN_ERROR,"系统异常");
        }

    }

    @PostMapping("/getRentalInfoList")
    @ApiOperation("查询押品租赁信息列表")
    public Map<String,Object> getRentalInfoList(@RequestBody ParamInfoListVO valueInfoListVO) {
        Assert.hasLength(valueInfoListVO.getCltId(),"押品编号丢失");
        return collBaseInfoService.getRentalInfoList(valueInfoListVO);

    }

    @GetMapping("/getRentalInfo")
    @ApiOperation("查询押品租赁信息")
    public Result getRentalInfo(@RequestParam String rentalinfoId) {
        try {
            Assert.hasLength(rentalinfoId,"id丢失");
            return Result.success(collBaseInfoService.getRentalInfo(rentalinfoId));
        }catch (IllegalArgumentException e){
            e.printStackTrace();
            return Result.error(ResultCode.PARAM_ERROR,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(ResultCode.UNKNOWN_ERROR,"系统异常");
        }

    }

    @PostMapping("/deleteRentalInfo")
    @ApiOperation("删除押品租赁信息")
    public Result deleteRentalInfo(@RequestBody List<String> rentalinfoIds) {
        try {
            return Result.success(collBaseInfoService.deleteRentalInfo(rentalinfoIds));
        }catch (IllegalArgumentException e){
            e.printStackTrace();
            return Result.error(ResultCode.PARAM_ERROR,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(ResultCode.UNKNOWN_ERROR,"系统异常");
        }
    }

    @GetMapping("/getRentalInfoByCltId")
    @ApiOperation("通过押品id查找租赁信息")
    public Map<String, Object> getRentalInfoByCltId(@RequestParam String cltId){
        Assert.hasLength(cltId, "押品ID丢失");
        Map<String, Object> Result = collBaseInfoService.getRentalInfoByCltId(cltId);
        return Result;
    }

    @PostMapping("/saveRentalInfo")
    @ApiOperation("保存租赁信息")
    public Result saveRentalInfo(@RequestBody RentalInfo rentalInfo){
            return Result.success(rentalInfoService.insert(rentalInfo));
    }

    @PostMapping("/updateRentalInfo")
    @ApiOperation("修改租赁信息")
    public Result updateRentalInfo(@RequestBody RentalInfo rentalInfo){
            return Result.success(rentalInfoService.update(rentalInfo));
    }

    @GetMapping("/delRentalInfo")
    @ApiOperation("删除押品租赁信息")
    public Result delRentalInfo(@RequestParam String rentalinfoId) {
        return Result.success(rentalInfoService.deleteByPrimaryKey(rentalinfoId));
    }

    @PostMapping("/getAccidentInfoList")
    @ApiOperation("查询押品日常管理信息列表")
    public Map<String,Object> getAccidentInfoList(@RequestBody ParamInfoListVO valueInfoListVO) {
        Assert.hasLength(valueInfoListVO.getCltId(),"押品编号丢失");
        return collBaseInfoService.getAccidentInfoList(valueInfoListVO);
    }

    @PostMapping("/deleteAccidentInfo")
    @ApiOperation("删除押品日常管理信息")
    public Result deleteAccidentInfo(@RequestBody List<String> accidentinfoIds) {
        try {
            return Result.success(collBaseInfoService.deleteAccidentInfo(accidentinfoIds));
        }catch (IllegalArgumentException e){
            e.printStackTrace();
            return Result.error(ResultCode.PARAM_ERROR,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(ResultCode.UNKNOWN_ERROR,"系统异常");
        }
    }

    @GetMapping("/getAccidentInfoByCltId")
    @ApiOperation("通过押品ID查找日常信息")
    public Map<String, Object> getAccidentInfoByCltId(@RequestParam String cltId){
        Assert.hasLength(cltId, "押品ID丢失");
        Map<String, Object> Result = collBaseInfoService.getAccidentInfoByCltId(cltId);
        return Result;
    }

    @PostMapping("/saveAccidentInfo")
    @ApiOperation("保存日常信息")
    public Result saveAccidentInfo(@RequestBody AccidentInfo accidentInfo){
            return Result.success(accidentInfoService.insert(accidentInfo));
    }

    @PostMapping("/updateAccidentInfo")
    @ApiOperation("修改日常信息")
    public Result updateAccidentInfo(@RequestBody AccidentInfo accidentInfo){
        return Result.success(accidentInfoService.update(accidentInfo));
    }

    @GetMapping("/delAccidentInfo")
    @ApiOperation("删除押品日常信息")
    public Result delAccidentInfo(@RequestParam String accidentinfoId) {
        return Result.success(accidentInfoService.deleteByPrimaryKey(accidentinfoId));
    }

    @GetMapping("/getDispositionInfo")
    @ApiOperation("查询押品处置信息")
    public Result getDispositionInfo(@RequestParam String cltId) {
        try {
            Assert.hasLength(cltId,"押品编号丢失");
            return Result.success(collBaseInfoService.getDispositionInfo(cltId));
        }catch (IllegalArgumentException e){
            e.printStackTrace();
            return Result.error(ResultCode.PARAM_ERROR,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(ResultCode.UNKNOWN_ERROR,"系统异常");
        }

    }

    @GetMapping("/delCoowner")
    @ApiOperation("删除共有人信息表")
    public Result delCoowner(@RequestParam String coownerId) {
        return Result.success(coownerService.deleteByPrimaryKey(coownerId));
    }

    @GetMapping("/getCoownerByCltId")
    @ApiOperation("通过押品编号查询共有人信息表")
    public Map<String, Object> getCoownerByCltId(@RequestParam String cltId) {
        Assert.hasLength(cltId,"押品主键丢失");
        Map<String, Object> result = collBaseInfoService.getCoownerByCltId(cltId);
        return result;
    }

    @PostMapping("/saveCoowner")
    @ApiOperation("保存押品共有人信息")
    public Result saveCoowner(@RequestBody Coowner coowner) {
            return Result.success(coownerService.insert(coowner));
    }

    @PostMapping("/updateCoowner")
    @ApiOperation("修改押品共有人信息")
    public Result updateCoowner(@RequestBody Coowner coowner) {
        return Result.success(coownerService.update(coowner));
    }

//    @PostMapping("/update")
//    @ApiOperation("更新共有人信息表")
//    public Result update(@RequestBody Coowner coowner) {
//        return Result.success(coownerService.update(coowner));
//    }

}
